<template>
	<view class="container">
		<image mode="scaleToFill" src="/static/image/universal/voice.png" @click.stop="clickImg" class="img"></image>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="base" :title="title" @confirm="confirm" @close="close"
				confirmText="开始" cancelText="结束" :before-close="true"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		uploadRecord
	} from "@/utils/request.js"
	export default {
		data() {
			return {
				text: undefined,
				title: "录音",
			}
		},
		methods: {
			setTitle() {
				let num = 0
				clearInterval(this.interval)
				this.interval = setInterval(() => {
					let text = "录音中"
					text = text.padEnd(num % 6 + 3, ".")
					this.title = text
					num++
				}, 200)
			},
			clickImg() {
				this.$refs.inputDialog.open()
			},
			confirm() {
				this.recorderManager = uni.getRecorderManager();
				console.log("this.recorderManager", this.recorderManager);
				this.recorderManager.onStop((res) => {
					this.voicePath = res.tempFilePath;
					uploadRecord("composite/ai/recognize", res.tempFilePath).then(data => {
						console.log(data, "00000000000000");
						data = JSON.parse(data)
						if (data.success && data.status === 1) {
							let text = data.result[0].result[0] || ""
							console.log(data.message, "texttexttexttext");
							console.log(text, "texttexttexttext");
							this.$emit("setText", text)
						} else {
							this.$message("转化失败", {
								icon: "error"
							})
						}
					})
				});
				this.recorderManager.start();
				this.setTitle()
			},
			close() {
				this.recorderManager?.stop();
				this.$refs.inputDialog.close()
				clearInterval(this.interval)
				this.title = "录音"
			},

		}
	}
</script>

<style scoped>
	.img {
		width: 48rpx;
		height: 48rpx;
	}
</style>
